import Mock from "mockjs";

let { data } = Mock.mock({
  // 20条数据
  "data|125": [
    {
      name: "@cname",
      "id|+1":1,
      "sex|1": ["男", "女"],
      "phone|13000000000-18999999999": 13000000000,
      "bh|10000000000000-99999999999999": 10000000000000,
      "dz|1": ["北国经典", "依山梅院", "三塘乐园", "天鹅湾"],
      "lx|1": ["一室", "两室", "三室"],
      "zt|1": ["已租", "未租"],
      "city|1":["上城区","拱墅区","下沙区","临平区","西湖区"],

    },
  ],
});

Mock.mock("/api/whole/list", "post", (options) => {
  const { pageSize, currentPage,search ,city,lx,zt} = JSON.parse(options.body);
  

  if (pageSize && currentPage) {
    let result;
    let count =0;
    
    if(search){
     result=data.filter((item:any)=>item.bh === +search)
     .slice((currentPage - 1) * pageSize,currentPage * pageSize);
      count=data.filter((item:any)=>item.bh===search).length;
    }else{
      
      result = data.slice((currentPage - 1) * pageSize,currentPage * pageSize);
      count=data.length
    }
    if(city&&lx&&zt){
      result=data.filter((item:any)=>item.city===city&&item.lx===lx&&item.zt===zt)
    }

    return {
      status: 0,
      result: result,
      count:count
    };
  } else {
    return {
      status: 1,
      result: "参数错误",
    };
  }
});

Mock.mock("/api/whole/del","post",(options)=>{
  const {id}=JSON.parse(options.body);
  if(id){
  data=data.filter((item:any)=>item.id!==id)
    return {
      status:0,
      msg:"删除成功"
    }
  }else{
    return {
      status:1,
      msg:"参数错误"
    }
  }
})


//添加
Mock.mock("/api/whole/add", "post", (options) => {
  const { datas } = JSON.parse(
    options.body
  );
  console.log(datas);
  
  if (datas) {
  data.unshift( datas)
  return{
    status:0,
  msg:"添加成功"
  }
  }else{
    return{
      status:1,
      msg:"参数错误,必须有姓名、收入和订单类型数据"
    }
  }
});
export default Mock;
